﻿using System;
using ArtMath.PointCollection;
using ArtMath.Component.Point;

namespace ArtMath.Component.Polygon
{
    /// <summary>
    /// 菱形
    /// </summary>
    [Serializable]
    public class Diamond : ParallelogramBase
    {
        public Diamond(SketchPad pad, string name, bool addToList = true) : base(pad, name, ToolTypeName.lx, addToList)
        {
        }
        public override void ComponentBuilding(Base component)
        {
            if (component == Line2)
            {
                Line2.AllowFix = false;
                Line2.SpareCoordinate_ = Pad.FixOnCircle(Line2.Point1.Pxy, Line1.Length, Line2.SpareCoordinate);
                Line2.AllowFix = true;
            }
            base.ComponentBuilding(component);
        }
        protected override PointCollectionBase[] collections()
        {
            return new PointCollectionBase[] { new PointCollection.Circle(Line2.Point1.ToPointD(), Line1.Length) };
        }
        public override PointCollectionBase[] GetInPointMovingRange(PointBase movePoint, Base sender = null)
        {
            switch (CountMovablePoints())
            {
                case 2:
                    if (movePoint == Line1.Point1)
                    {
                        if (Line2.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line4.Point1.NewXY, Line3.NewLength) };
                        }
                        else if (Line3.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine((Line2.Point1.NewXY + Line4.Point1.NewXY) / 2, -1 / Line2.Point1.NewXY.GetSlope(Line4.Point1.NewXY)) };
                        }
                        else if (Line4.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line2.Point1.NewXY, Line2.NewLength) };
                        }
                    }
                    else if (movePoint == Line2.Point1)
                    {
                        if (Line1.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line3.Point1.NewXY, Line3.NewLength) };
                        }
                        else if (Line3.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line1.Point1.NewXY, Line4.NewLength) };
                        }
                        else if (Line4.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine((Line1.Point1.NewXY + Line3.Point1.NewXY) / 2, -1 / Line1.Point1.NewXY.GetSlope(Line3.Point1.NewXY)) };
                        }
                    }
                    else if (movePoint == Line3.Point1)
                    {
                        if (Line1.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine((Line2.Point1.NewXY + Line4.Point1.NewXY) / 2, -1 / Line2.Point1.NewXY.GetSlope(Line4.Point1.NewXY)) };
                        }
                        else if (Line2.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line4.Point1.NewXY, Line4.NewLength) };
                        }
                        else if (Line4.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line2.Point1.NewXY, Line1.NewLength) };
                        }
                    }
                    else if (movePoint == Line4.Point1)
                    {
                        if (Line1.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line3.Point1.NewXY, Line2.NewLength) };
                        }
                        else if (Line2.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new StraightLine((Line1.Point1.NewXY + Line3.Point1.NewXY) / 2, -1 / Line1.Point1.NewXY.GetSlope(Line3.Point1.NewXY)) };
                        }
                        else if (Line3.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new PointCollection.Circle(Line1.Point1.NewXY, Line1.NewLength) };
                        }
                    }
                    break;
            }
            return base.GetInPointMovingRange(movePoint, sender);
        }
    }
}
